srvMap.add('selectIndicatorAssembleCount', 'AssembleCount.json','front/sh/indicatorStore!execute?uid=store_010');//查询指标组装单数
srvMap.add('selectIndicatorInfo', 'indexInfo.json','front/sh/indicatorStore!execute?uid=store_004');//查询指标详细信息
srvMap.add('addIndicatorAssembleCount', '','front/sh/indicatorStore!execute?uid=store_009');//加入指标组装单
srvMap.add('showIndexDetailInfo', 'indexDetailInfo.json','front/sh/indicatorStore!execute?uid=store_011');//指标详细信息
srvMap.add('changeIndexVersion', 'indexInfoByVol.json','front/sh/indicatorStore!execute?uid=store_012');//切换指标版本详细信息

srvMap.add('selectIndicatorAttenFolder', 'selectIndicatorAttenFolder.json','front/sh/indicatorStore!execute?uid=store_006');//查询用户关注指标分类
srvMap.add('doIndicatorAttenOrCancel', 'doIndicatorAttenOrCancel.json','front/sh/indicatorStore!execute?uid=store_005');//关注或取消关注某指标
srvMap.add('createIndicatorAttenFolder', 'createIndicatorAttenFolder.json','front/sh/indicatorStore!execute?uid=store_002');//用户创建关注指标分类

var VERSION_INFO = [];
var KEYWORD = (Util.Url.GetArgsFromHref("this", "keyWord"));
//var KEYWORD= "";
var PAGENUM = 1;
/**
 * 指标超市
 * @author suyao
 */
Idx_Store = {
		/**
		 * 页面分页查询方法
		 */
		init : function(){
			$('.pagination-first').trigger("click");
			var keyWord1 = $('#indexKeyWord').val();
//			if(keyWord1 == "请输入关键字"){
//				keyWord1 = "";
//			}
			if((keyWord1 == null || keyWord1 == "") && !!KEYWORD){
				keyWord1 = decodeURI(KEYWORD);
				$('#indexKeyWord').val(keyWord1);
			}
			var COUNT = 10;    //设置分页默认每页展示条数
			$('#pagenationStyle').pagination({
			    total:Number(Idx_Store.selectIndexCount(keyWord1)),
			    pageSize:COUNT,
			    pageList:[10,15,20],
			    beforePageText : "第",
				afterPageText : "页 共 {pages} 页",
				displayMsg : "总记录 {total} 条，当前显示第 {from} 到 {to} 条",
			    onSelectPage: function(pageNumber, pageSize){
//			    	alert(keyWord)
			    	PAGENUM = pageNumber;
			    	Idx_Store.selectIndicatorsInfo(pageNumber,pageSize,keyWord1);
			    },
			    onRefresh : function(pageNumber, pageSize) {
//			    	alert("refresh")
			    	PAGENUM = pageNumber;
			    	Idx_Store.selectIndicatorsInfo(pageNumber,pageSize,keyWord1);
				}
			    
			});
			//查询方法
			Idx_Store.selectIndicatorsInfo(PAGENUM,COUNT,keyWord1);
		} ,	
		/**
		 * 排序
		 * @param obj
		 */
		sortIdxInfo : function(obj){
			var className = $(obj).attr("class");
			if(className == "sortnorm"){  //切换排序规则
				$(".sort").find("li").each(function(){
					$(this).attr("class","sortnorm");
				});
				$(obj).attr("class","sortOn");
			}else{
				if(className == "sortOn"){
					$(obj).attr("class","sortDown");
//						sortType = "asc";
				}else if(className == "sortDown"){
					$(obj).attr("class","sortOn");
				}
			}
			//指标信息查询方法
			Idx_Store.init();
		},
		/**
		 * 查询指标组装单数
		 */
		selectIndicatorAssembleCount : function(){
			var par = "&userId="+USER_ID;
			Util.ajax.postJsonSync(srvMap.get('selectIndicatorAssembleCount'),par,function(json,status){
				if(status){
					$(".assemblyNumber").text(json.object.countNum);
				}else{
					 $.messager.alert('错误','数据异常！');   
				}
			});
		},
		/**
		 * 获取查询指标数据
		 */
		getSelectIndexArray : function(){
			var queryArray = [];
			$("#selectTip").find("li").each(function(){
				var jsonObj = new Object();
				
				jsonObj.key = $(this).find('p').attr('value');
				jsonObj.value = $(this).find('p').attr('id');
				jsonObj.partFlag = $(this).find('span').attr('value');
				
				queryArray.push(jsonObj);
			});
			queryArray = JSON.stringify(queryArray);
			return queryArray;
		},
		/**
		 * 获取排序
		 * @returns {___anonymous3800_3806}
		 */
		getSortInfo : function(){
			var sortStr = "";
			var sortType = "";
			$(".sort").find("li").each(function(){
				var className = $(this).attr("class");
				if(className != "sortnorm"){
					var sortId = $(this).attr("id");
					if(className == "sortOn"){
						sortType = "desc";
					}else{
						sortType = "asc";
					}
					switch (sortId) {
					case "sortByTime":  //"最近"
						sortStr = "createTime";
						break;
					case "sortByCollection":  //"收藏"
						sortStr = "userCnt";
						break;
					default:   //"点击"
						sortStr = "visitCnt";
						break;
					}
				}
			});
			var sortObj = new Object();
			sortObj.sortStr = sortStr;
			sortObj.sortType = sortType;
			
			return sortObj;
		},
		/**
		 * 指标查询方法
		 * @param pageNumber
		 * @param pageSize
		 */
		selectIndicatorsInfo : function(pageNumber,pageSize,keyWord1){
			$('#div_waterMark').show();
			$('#div_loader').show();
			
			var indexSelectArray = Idx_Store.getSelectIndexArray();
			var sortStr = Idx_Store.getSortInfo().sortStr;
			var sortType = Idx_Store.getSortInfo().sortType;
//			alert(KEYWORD)
//			alert(keyWord1)
			if(keyWord1 != KEYWORD){
				keyWord1 = encodeURIComponent(keyWord1);   
				keyWord1 = keyWord1.replace(/%5C/g, "%5C%5C");
				keyWord1 = keyWord1.replace(/%22/g, "%5C%22");
				keyWord1 = keyWord1.replace(/%0A/g, "");
				keyWord1 = keyWord1.replace(/%25/g, "%5C%25");
				
				var par = "&userId="+USER_ID+"&indexSelectArray="+indexSelectArray+"&currentPage="+pageNumber+"&pageSize="+pageSize+"&sortStr="+sortStr+"&sortType="+sortType;
				par += "&paraName="+keyWord1;
			}else{
				KEYWORD = encodeURIComponent(KEYWORD);   
				KEYWORD = KEYWORD.replace(/%5C/g, "%5C%5C");
				KEYWORD = KEYWORD.replace(/%22/g, "%5C%22");
				KEYWORD = KEYWORD.replace(/%0A/g, "");
				KEYWORD = KEYWORD.replace(/%25/g, "%5C%25");
				
				var par = "&userId="+USER_ID+"&indexSelectArray="+indexSelectArray+"&currentPage="+pageNumber+"&pageSize="+pageSize+"&sortStr="+sortStr+"&sortType="+sortType;
				par += "&paraName="+KEYWORD;
			}
//						alert(par)
			Util.ajax.postJsonSync(srvMap.get('selectIndicatorInfo'),par,function(json,status){
				$('#div_waterMark').hide();
				$('#div_loader').hide();
				if(status){
					if(json.bean.dataTotal == "0"){
						$('#noquery_common').show();
					}else{
						$('#noquery_common').hide();
					}
					Util.ajax.loadTemp('.inspectDetl',$('#IDX_DET_INFO'),json.beans);
					$('.classifyTotal').find('span').text(json.bean.dataTotal);
					
//					Idx_Store.lightKeyWord();
					var key = $('#indexKeyWord').val();
					$(".inspectDetl").textSearch(key,{markColor: "blue"});
					
//					$('.inspectDetl').find('a').each(function(){
//						alert(11)
//						$(this).textSearch(key,{markColor: "blue"});
//					});
				}else{
					$.messager.alert('错误','数据异常！');   
				}
			});
		},
		/**
		 * 指标个数
		 * @returns {___anonymous_indexCount}
		 */
		selectIndexCount : function(keyWord1){
			var indexSelectArray = Idx_Store.getSelectIndexArray();
			var sortStr = Idx_Store.getSortInfo().sortStr;
			var sortType = Idx_Store.getSortInfo().sortType;
			var pageNumber = 1;
			var pageSize = 10;
			if(keyWord1 != KEYWORD){
				keyWord1 = encodeURIComponent(keyWord1);   //业务口径
				keyWord1 = keyWord1.replace(/%5C/g, "%5C%5C");
				keyWord1 = keyWord1.replace(/%22/g, "%5C%22");
				keyWord1 = keyWord1.replace(/%0A/g, "");
				
				var par = "&userId="+USER_ID+"&indexSelectArray="+indexSelectArray+"&currentPage="+pageNumber+"&pageSize="+pageSize+"&sortStr="+sortStr+"&sortType="+sortType;
				par += "&paraName="+keyWord1;
			}else{
				KEYWORD = encodeURIComponent(KEYWORD);   //业务口径
				KEYWORD = KEYWORD.replace(/%5C/g, "%5C%5C");
				KEYWORD = KEYWORD.replace(/%22/g, "%5C%22");
				KEYWORD = KEYWORD.replace(/%0A/g, "");
				
				var par = "&userId="+USER_ID+"&indexSelectArray="+indexSelectArray+"&currentPage="+pageNumber+"&pageSize="+pageSize+"&sortStr="+sortStr+"&sortType="+sortType;
				par += "&paraName="+KEYWORD;
			}
//			alert(par)
			Util.ajax.postJsonSync(srvMap.get('selectIndicatorInfo'),par,function(json,status){
				if(status){
					indexCount = json.bean.dataTotal;
				}else{
					$.messager.alert('错误','数据异常！');   
				}
			});
			return indexCount;
		},
		/**
		 * 展示指标详细信息
		 * @param idxId
		 */
		elementDisplay : function(idxId){
			var idxVol = $('#detInfo'+idxId+'').attr('value');
			var par = "&idxId="+idxId;
			Util.ajax.postJsonSync(srvMap.get('showIndexDetailInfo'),par,function(json,status){
				if(status){
					VERSION_INFO = json.object.VERSION_INFO;
					
					//对version_code进行去重
					for(var i=0;VERSION_INFO.length>i;i++){
						for(var j=i+1;VERSION_INFO.length>j;j++){
							if(VERSION_INFO[i].versionCode == VERSION_INFO[j].versionCode){
								VERSION_INFO.splice(j,1);
							}else{
								j++;
							}
						}
					}
					json.object.NEW_VERSION_DETAILINFO.versionInfo = VERSION_INFO;
					if(json.object.NEW_VERSION_DETAILINFO.dimName.length>15){
						json.object.NEW_VERSION_DETAILINFO.dimNameMini = json.object.NEW_VERSION_DETAILINFO.dimName.substring(0,10)+"...";
					}else{
						json.object.NEW_VERSION_DETAILINFO.dimNameMini = json.object.NEW_VERSION_DETAILINFO.dimName;
					}
					
					if(json.object.NEW_VERSION_DETAILINFO.idxFolderPath.length>15){
						json.object.NEW_VERSION_DETAILINFO.idxFolderPathMini = json.object.NEW_VERSION_DETAILINFO.idxFolderPath.substring(0,10)+"...";
					}else{
						json.object.NEW_VERSION_DETAILINFO.idxFolderPathMini = json.object.NEW_VERSION_DETAILINFO.idxFolderPath;
					}
					
					if(json.object.NEW_VERSION_DETAILINFO.sourceTableName.length>15){
						json.object.NEW_VERSION_DETAILINFO.sourceTableNameMini = json.object.NEW_VERSION_DETAILINFO.sourceTableName.substring(0,10)+"...";
					}else{
						json.object.NEW_VERSION_DETAILINFO.sourceTableNameMini = json.object.NEW_VERSION_DETAILINFO.sourceTableName;
					}
					
					if(json.object.NEW_VERSION_DETAILINFO.columnName.length>15){
						json.object.NEW_VERSION_DETAILINFO.columnNameMini = json.object.NEW_VERSION_DETAILINFO.columnName.substring(0,10)+"...";
					}else{
						json.object.NEW_VERSION_DETAILINFO.columnNameMini = json.object.NEW_VERSION_DETAILINFO.columnName;
					}
					
					var detailInfoArray = [];
					detailInfoArray.push(json.object.NEW_VERSION_DETAILINFO);
//					alert(JSON.stringify(detailInfoArray))
					
					Util.ajax.loadTemp('#indexInfoDetail',$('#IDX_DETAIL_INFO'),detailInfoArray);
					$("#indexInfoDetail").show();
				}else{
					$.messager.alert('错误','查询异常！');   
				}
			});
		},
		/**
		 * 指标详细信息切换版本
		 * @param obj
		 * @param idxVol
		 */
		changeIndexVersion : function(obj,idxVol){
			var idxId = $(obj).parent().attr("value");
			var par = "&idxId="+idxId+"&idVol="+idxVol;
			Util.ajax.postJsonSync(srvMap.get('changeIndexVersion'),par,function(json,status){
				if(status){
					
					//TODO
					var $par = $('#detInfo'+idxId+'');
					$par.attr('value',json.object.versionCode);    //版本号
					$par.find('div').eq(0).find('h2').eq(0).text("指标业务口径："+json.object.busiRule);   //指标业务口径
					$par.find('div').eq(0).find('h2').eq(1).text("维度："+json.object.dimName);   //维度
					
					json.object.versionInfo = VERSION_INFO;
					
					if(json.object.dimName.length>15){
						json.object.dimNameMini = json.object.dimName.substring(0,10)+"...";
					}else{
						json.object.dimNameMini = json.object.dimName;
					}
					
					if(json.object.idxFolderPath.length>15){
						json.object.idxFolderPathMini = json.object.idxFolderPath.substring(0,10)+"...";
					}else{
						json.object.idxFolderPathMini = json.object.idxFolderPath;
					}
					
					if(json.object.sourceTableName.length>15){
						json.object.sourceTableNameMini = json.object.sourceTableName.substring(0,10)+"...";
					}else{
						json.object.sourceTableNameMini = json.object.sourceTableName;
					}
					
					if(json.object.columnName.length>15){
						json.object.columnNameMini = json.object.columnName.substring(0,10)+"...";
					}else{
						json.object.columnNameMini = json.object.columnName;
					}
					
					var indexInfoByVol = [];
					indexInfoByVol.push(json.object);
					Util.ajax.loadTemp('#indexInfoDetail',$('#IDX_DETAIL_INFO'),indexInfoByVol);
					
				}else{
					$.messager.alert('错误','查询异常！');   
				}
			});
		},
		/**
		 * 加入指标组装单Btn
		 * @param idxId
		 * @param obj
		 */
		addIndexAssemble : function(idxId,obj){
			$(obj).attr("class","buttonAdd_already");
			$(obj).text("进入组装单");
			$(obj).attr('onclick',"location.href='../indexAssembly/indexAssembly.html'");
			var par = "&idxId="+idxId+"&userId="+USER_ID;
			Util.ajax.postJsonSync(srvMap.get('addIndicatorAssembleCount'),par,function(json,status){
				if(status){
					Idx_Store.selectIndicatorAssembleCount();
				}else{
					$.messager.alert('错误','操作异常！');   
				}
			});
		},
		/**
		 * 查询用户关注指标分类
		 * @param obj
		 * @param idxId
		 */
		showAtten : function(obj,idxId){
			var par = "&userId="+USER_ID;
			Util.ajax.postJsonSync(srvMap.get('selectIndicatorAttenFolder'),par,function(json,status){
				if(status){
					Util.ajax.loadTemp('#attenFolder'+idxId+'',$('#IDX_USER_ATTEN'),json.object);
				}else{
					$.messager.alert('错误','查询异常！');   
				}
			});
			
			$('.pop_attetion').hide();
			$(obj).parent().find('div').show();
		},
		/**
		 * 显示分类
		 */
		showAttenFolder : function(obj){
			var attenFolderText = $(obj).text();
//			alert(attenFolderText)
			if(attenFolderText == "分类到"){
				$(obj).text("收回");
				$(obj).parent().find('span').show();
			}else{
				$(obj).text("分类到");
				$(obj).parent().find('span').hide();
			}
		},
		/**
		 * 取消关注
		 * @param obj
		 * @param idxId
		 */
		cancelAtten : function(obj,idxId){
			//指标分类Id
			var attenId = "";
			//取消关注
			var type = 2;
			var version = $(obj).parent().parent().attr('value');
			var par = "&userId="+USER_ID+"&indicatorId="+idxId+"&attenFolderId="+attenId+"&type="+type+"&versionCode="+version;
			Util.ajax.postJsonSync(srvMap.get('doIndicatorAttenOrCancel'),par,function(json,status){
				if(status){
					$(obj).hide();
					$('#addAtten'+idxId+'').show();
				}else{
					$.messager.alert('错误','操作异常！');   
				}
			});
		},
		/**
		 * 确认关注
		 * @param obj
		 * @param idxId
		 */
		addAtten : function(obj,idxId){
			var type = 1;
			var attenFolderId = "";
			var version = $(obj).parent().parent().parent().attr('value2');
//			alert(version)
			$("#attenFolder"+idxId+"").find("p").each(function(){
				if($(this).find('input').is(':checked')){
					attenFolderId = $(this).find('input').val();
				}
			});
			
//			if(attenFolderId != "" && attenFolderId != undefined){
				var par = "&userId="+USER_ID+"&indicatorId="+idxId+"&attenFolderId="+attenFolderId+"&type="+type+"&versionCode="+version;
				Util.ajax.postJsonSync(srvMap.get('doIndicatorAttenOrCancel'),par,function(json,status){
					if(status){
						$('#addAtten'+idxId+'').hide();
						$('#cancelAtten'+idxId+'').show();
						
						$(obj).parent().find('div').find('span').hide();
						$(obj).parent().find('div').find('i').text("分类到");
						
						$(obj).parent().hide();
					}else{
						$.messager.alert('错误','操作异常！');   
					}
				});
//			}else{
//				$.messager.alert('错误','请选择一个分类！'); 
//			}
		},
		/**
		 * 添加分类
		 * @param obj
		 * @param idxId
		 */
		addAttenClass : function(obj,idxId){
			$('#addAttenPlace').remove("");
			
			var strHtml ="";
			strHtml += '<p id="addAttenPlace">';
			strHtml += '<input id="addAtten" type="text" value="" placeholder="最多6个字符" maxlength="6" style="width:67px"/>';
			strHtml += '<span onclick="Idx_Store.submitAttenClass(this);">添加</span></p>';
			$('#attenFolder'+idxId+'').append(strHtml);
			
			//限制输入特殊字符
			$('#addAtten').attr("onkeyup","value=value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5]/g,'')");
			$('#addAtten').attr("onpaste","value=value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5]/g,'')");
			$('#addAtten').attr("oncontextmenu","value=value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5]/g,'')");
			
			$('#addAtten').keyup(function(event){
				var addAttenName = $('#addAtten').val(); 
				 var e = event || window.event || arguments.callee.caller.arguments[0];
			     if((e && e.keyCode==13)){ // enter键
						var attenFolderName = cTrim(addAttenName);
						if(attenFolderName != "" && attenFolderName != undefined){
							var par = "&userId="+USER_ID+"&attenFolderName="+attenFolderName;
							Util.ajax.postJsonSync(srvMap.get('createIndicatorAttenFolder'),par,function(json,status){
								if(status){
									//移除以往的指标关注类型
//									$('#attenFolder'+idxId+'').empty();
									if(json.bean.resultCode == "2"){
										$.messager.alert('警告','分类命名重复！'); 
									}else{
										//重新加载该用户关注的指标类型
										var param = "&userId="+USER_ID;
										Util.ajax.postJsonSync(srvMap.get('selectIndicatorAttenFolder'),param,function(json,status){
											if(status){
												Util.ajax.loadTemp('#attenFolder'+idxId+'',$('#IDX_USER_ATTEN'),json.object);
											}else{
												$.messager.alert('错误','查询异常！');   
											}
										});
									}
								}else{
									$.messager.alert('错误','操作异常！');   
								}
							});
						}
				}
			});
		},
		submitAttenClass : function(obj){
			var idxId = $(obj).parent().parent().attr('value');
			//TODO
			var attenFolderName = cTrim($(obj).parent().find('input').val());
			if(attenFolderName != "" && attenFolderName != undefined){
				var par = "&userId="+USER_ID+"&attenFolderName="+attenFolderName;
				Util.ajax.postJsonSync(srvMap.get('createIndicatorAttenFolder'),par,function(json,status){
					if(status){
						//移除以往的指标关注类型
//						$('#attenFolder'+idxId+'').empty();
						if(json.bean.resultCode == "2"){
							$.messager.alert('警告','分类命名重复！'); 
						}else{
							//重新加载该用户关注的指标类型
							var param = "&userId="+USER_ID;
							Util.ajax.postJsonSync(srvMap.get('selectIndicatorAttenFolder'),param,function(json,status){
								if(status){
									Util.ajax.loadTemp('#attenFolder'+idxId+'',$('#IDX_USER_ATTEN'),json.object);
								}else{
									$.messager.alert('错误','查询异常！');   
								}
							});
						}
					}else{
						$.messager.alert('错误','操作异常！');   
					}
				});
			}
		},
		
}

/**
 * 页面加载
 */
$(document).ready(function(){
	
	$(".allclass").hover(function() {
		$(".subsety_tabInd").css("display", "block");
	}, function() {
		$(".subsety_tabInd").css("display", "none");
	});
	
	/**
	 * 定义handlebars help
	 */
	Handlebars.registerHelper('if_even', function(value, options) {
		  if(value == 2) {
		    return options.fn(this);
		  } else {
		    return options.inverse(this);
		  }
		});
	
	//分页查询
	Idx_Store.init();
	//查询指标组装单个数
	Idx_Store.selectIndicatorAssembleCount();
	
});


